#ifndef LAB_MAIN_H
#define LAB_MAIN_H

#include "pub_pop_get_date.h"
#include "pub_pop_get_item.h"

#include <QWidget>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QTimer>

class CustomDelegate : public QStyledItemDelegate {
public:

    QWidget *createEditor(QWidget *parent,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index)
    const override
    {
        // 创建自定义编辑器，例如 QComboBox
        QComboBox *editor = new QComboBox(parent);
        editor->addItem("阴性");
        editor->addItem("阳性");
        //editor->addItem("");
        editor->setEditable(true);
        return editor;
    }

    //QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) ;

    void setEditorData(QWidget *editor, const QModelIndex &index)
    const override
    {
        // 设置编辑器的数据，例如从数据模型获取数据并在编辑器中显示
        QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
        if (comboBox)
        {
            QString value = index.model()->data(index, Qt::EditRole).toString();
            comboBox->setCurrentText(value);
        }
    }

    void setModelData(QWidget *editor,
                      QAbstractItemModel *model,
                      const QModelIndex &index)
    const override
    {
        // 更新数据模型的数据，例如从编辑器获取数据并保存到数据模型
        QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
        if (comboBox)
        {
            QString value = comboBox->currentText();
            model->setData(index, value, Qt::EditRole);
        }
    }

    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index)
    const override
    {
        // 更新编辑器的位置和大小
        editor->setGeometry(option.rect);
    }
};


namespace Ui {
class Lab_main;
}

class Lab_main : public QWidget
{
    Q_OBJECT

public:
    explicit Lab_main(QWidget *parent = nullptr);
    ~Lab_main();

    void closeEvent(QCloseEvent *event);

signals:
    void destroyW( QString );

private slots:

    void calculate_group_price(QString);

    void tmr_timeout();

    void clear_base();
    void clear_group();
    void clear_rule();

    void on_pb_close_clicked();

    void on_tabWidget_tabBarClicked(int index);

    void on_tv_dict_clicked(const QModelIndex &index);

    void on_tv_dict_doubleClicked(const QModelIndex &index);

    void on_tv_value_clicked(const QModelIndex &index);

    void on_tv_value_doubleClicked(const QModelIndex &index);

    void on_pb_add_clicked();

    void on_pb_del_clicked();

    void on_pb_add_base_clicked();

    void on_tv_machine_list_clicked(const QModelIndex &index);

    void on_tv_machine_list_doubleClicked(const QModelIndex &index);

    void on_tv_item_list_base_clicked(const QModelIndex &index);

    void on_tv_item_list_base_doubleClicked(const QModelIndex &index);

    void on_pb_del_base_clicked();

    void on_tb_reg_0_clicked();

    void on_pb_list_patient_clicked();

    void on_tv_group_list_clicked(const QModelIndex &index);

    void on_tv_group_list_doubleClicked(const QModelIndex &index);

    void on_tv_group_item_clicked(const QModelIndex &index);

    void on_tv_group_item_doubleClicked(const QModelIndex &index);

    void on_pb_load_group_clicked();

    void on_pb_del_group_clicked();

    void on_pb_add_group_clicked();

    void on_pb_load_price_item_clicked();

    void on_pb_item_change_clicked();

    void on_pb_save_ord_clicked();

    void on_pb_add_rule_clicked();

    void on_tv_machine_list_rule_clicked(const QModelIndex &index);

    void on_tv_machine_list_rule_doubleClicked(const QModelIndex &index);

    void on_pb_get_item_rule_clicked();

    void on_tv_item_list_rule_clicked(const QModelIndex &index);

    void on_pb_del_rule_clicked();

    void on_pb_item_change_rule_clicked();

    void on_pb_clear_clicked();

    void on_tv_list_patient_clicked(const QModelIndex &index);

    void on_tv_list_patient_doubleClicked(const QModelIndex &index);

    void on_pb_save_result_clicked();

    void on_rb_unsaved_clicked();

    void on_rb_saved_clicked();

private:
    Ui::Lab_main *ui;

    QSqlQuery *qrm, *qro, *qrot;
    QString sq;
    QSqlQueryModel *md_dict, *md_value; //字典
    QSqlQueryModel *md_mac, *md_mac_item;//明细项目
    QSqlQueryModel *md_group_list, *md_group_item;//组套项目
    QSqlQueryModel *md_mac_rule, *md_item_rule;//规则
    QSqlQueryModel *md_patient_list;//患者

    QSqlQueryModel *md_pop; //弹出

    QStandardItemModel * md_input;

    QModelIndex mdidx_dict, mdidx_base, mdidx_group, mdidx_rule;  //md_dict 索引

    QTimer *tmr;

    pub_pop_get_date * dlg_get_date;
    Pub_pop_get_item * dlg_getItem;

    CustomDelegate dlgt;

    QMap<QString,QString> patient_info;

    QStringList prepared_result;

};

#endif // LAB_MAIN_H
